import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28463
 * Date: 2022—07—08
 * Time: 13:06
 */
public class Demo01 {
    static class Node{
        public int val;
        public Node next;
        public Node(int val) {
            this.val = val;
        }
    }
    public Node head;
    public void printList(Node head) {
        if (head == null) {
            return;
        }
        if (head.next == null) {
            System.out.print(head.val + " ");
            return;
        }
        printList(head.next);
        System.out.print(head.val + " ");
    }
    public void printList2(Node head) {
        Stack<Node> stack = new Stack<>();
        Node cur = head;
        //将元素全部依次放入栈中
        while(cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        //给栈pop取出栈顶元素然后拿出val打印
        while(!stack.empty()) {
            Node top = stack.pop();
            System.out.println(top.val + " ");
        }
    }
}
